﻿Imports System.Windows.Media.Animation

Public Class ContextMenuWindow
    Private Location As New Point(0, 0)
    Private WithEvents InfoWindow As InformationWindow
    Private HoveredOnce As Boolean
    Public Sub New(MenuItems As Dictionary(Of String, Action), Location As Point, Menu As InformationWindow)
        InitializeComponent()
        Init(MenuItems, Location)
        InfoWindow = Menu
        Dim HideAfterInfoHover As New Timers.Timer(500)
        AddHandler HideAfterInfoHover.Elapsed, Sub()
                                                   Dispatcher.Invoke(Sub()
                                                                         If CursorWithinAllMenus() = False AndAlso HoveredOnce = True Then
                                                                             CloseWindow()
                                                                         ElseIf CursorWithinAllMenus() = True AndAlso HoveredOnce = False Then
                                                                             HoveredOnce = True
                                                                         End If
                                                                     End Sub)
                                               End Sub
        HideAfterInfoHover.Start()
    End Sub
    Function CursorWithinAllMenus() As Boolean
        Dim Radius As New System.Drawing.Rectangle(InfoWindow.Left, InfoWindow.Top, InfoWindow.Width, InfoWindow.Top + InfoWindow.Height + Me.Height)
        Return If(Forms.Cursor.Position.X >= Radius.Left And Forms.Cursor.Position.X <= Radius.Left + Radius.Width And Forms.Cursor.Position.Y >= Radius.Top And Forms.Cursor.Position.Y <= Radius.Top + Radius.Height, True, False)
    End Function
    Public Sub New(MenuItems As Dictionary(Of String, Action), Location As Point)
        InitializeComponent()
        Init(MenuItems, Location)
    End Sub

    'Public Sub New(MenuItems As Dictionary(Of String, Action))

    '    ' This call is required by the designer.
    '    InitializeComponent()

    '    ' Add any initialization after the InitializeComponent() call.
    '    Init(MenuItems, New Point((Forms.Cursor.Position.X - Me.ActualWidth) + 10, Forms.Cursor.Position.Y - 10))
    'End Sub

    Private Sub Init(MenuItems As Dictionary(Of String, Action), Location As Point)
        Me.Location = Location
        Me.Height = 0
        Me.Background = DirectCast(MyProperties.DockWindow.PinStackPanel.Background, SolidColorBrush)
        TheGrid.Background = DirectCast(MyProperties.DockWindow.PinStackPanel.Background, SolidColorBrush)

        For Each Item As Generic.KeyValuePair(Of String, Action) In MenuItems
            Dim MenuItem As New ContextMenuItem(Item.Value) With {.Text = Item.Key}
            AddHandler MenuItem.Clicked, AddressOf CloseWindow
            ItemPanel.Children.Add(MenuItem)
        Next

        Dim HeightAnimation As New DoubleAnimation(MenuItems.Count * 26, TimeSpan.FromSeconds(0.2)) With {.EasingFunction = New CircleEase With {.EasingMode = EasingMode.EaseOut}}
        Me.BeginAnimation(Window.HeightProperty, HeightAnimation)
    End Sub

    Private Sub ContextMenuWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
        Me.Left = Location.X
        Me.Top = Location.Y
    End Sub

    Public Sub CloseWindow() Handles Me.MouseLeave, InfoWindow.MouseLeave
        Dim SB As New Storyboard With {.Duration = TimeSpan.FromSeconds(0.2)}
        AddHandler SB.Completed, AddressOf Me.Close
        Dim HeightAnimation As New DoubleAnimation(0, SB.Duration) With {.EasingFunction = New CircleEase With {.EasingMode = EasingMode.EaseOut}}
        SB.Children.Add(HeightAnimation)
        Storyboard.SetTarget(HeightAnimation, Me)
        Storyboard.SetTargetProperty(HeightAnimation, New PropertyPath(Window.HeightProperty))
        SB.Begin()
    End Sub

End Class
